package net.wzw.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.wzw.entity.UserLoginEntity;
import net.wzw.entity.UserRegisterEntity;
import net.wzw.entity.UserRequestEntity;
import net.wzw.enums.BizCodes;
import net.wzw.service.FileService;
import net.wzw.service.UserService;
import net.wzw.utils.ApiResult;
import net.wzw.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wzw
 * @since 2024-08-11
 */
@Api(tags = "用户微服务账号模块")
@RestController
@RequestMapping("/api/user/v1/")
public class UserController {

    /**
     * 注入用户服务
     */
    @Autowired
    private UserService userService;

    /**
     * 注入文件上传服务
     */
    @Autowired
    private FileService fileService;

    @ApiOperation("用户账号信息查询")
    @GetMapping("/userDetail")
    public Object userDetail() {
        // 测试 swagger 效果
//        String userInfo ="{'id':1,'name':'wzw','sex':'男','mail':'1030181803@qq.com','avatar':'https://img1.baidu.com/it/u=1427121515,4062087528&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750'}";
//        return ApiResult.doSuccess(JSONObject.parseObject(userInfo));

        UserVO userInfo = userService.getUserDetail();
        return ApiResult.doSuccess(userInfo);
    }

    /**
     * 上传用户头像
     * 默认文件大小 1M，超过报错
     * @param file
     * @return
     */
    @ApiOperation("用户头像上传")
    @PostMapping("/upload")
    public ApiResult uploadUserAvatar(
            @ApiParam(value = "文件上传", required = true)
            @RequestPart("file") MultipartFile file) {
         String result = fileService.uploadUserAvatar(file);
         return result != null ? ApiResult.doSuccess(result) : ApiResult.doResult(BizCodes.USER_AVATAR_FILE_UPLOAD_ERROR);
    }

    /**
     * 用户注册
     * @param userRegisterEntity
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ApiResult register(@ApiParam("用户注册请求实体") @RequestBody UserRegisterEntity userRegisterEntity) {
        ApiResult result = userService.register(userRegisterEntity);
        return result;
    }

    /**
     * 用户登录
     * @param userLoginEntity
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ApiResult login(@ApiParam("用户登录请求实体") @RequestBody UserLoginEntity userLoginEntity) {
        ApiResult result = userService.login(userLoginEntity);
        return result;
    }

    /**
     * 用户刷新 token
     * @param refresh_token
     * @param access_token
     * @return
     */
    @ApiOperation("用户刷新token")
    @PostMapping("/refresh")
    public ApiResult refreshToken(@ApiParam(value = "刷新 token")  @RequestParam(value = "refresh_token") String refresh_token,
                                  @ApiParam(value = "访问 token") @RequestParam(value = "access_token") String access_token) {
        Map<String, Object> params = new HashMap<>();
        params.put("refresh_token", refresh_token);
        params.put("access_token", access_token);
        ApiResult result = userService.refreshToken(params);
        return result;
    }

    /**
     * 用户信息修改
     * @param userRequestEntity
     * @return
     */
    @ApiOperation("用户信息修改")
    @PostMapping("/save")
    public ApiResult save(@ApiParam("用户信息请求实体") @RequestBody UserRequestEntity userRequestEntity) {
        userService.save(userRequestEntity);
        return ApiResult.doSuccess();
    }

}

